package com.leha.mp.core.services.imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.leha.mp.core.constants.BizConstants;
import com.leha.mp.core.constants.CoreErrorDict;
import com.leha.mp.core.dao.PlConsultingMapper;
import com.leha.mp.core.entity.mybatis.PlConsulting;
import com.leha.mp.core.entity.mybatis.PlConsultingExample;
import com.leha.mp.core.services.ErrorMessageServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yanming
 * @date 2018/07/14
 */

@Service
public class ValidateService {

    @Autowired
    PlConsultingMapper consultingMapper;

    @Autowired
    ErrorMessageServices messageServices;

    /**
     * 检查用户所选的咨询时间是否合法与重复
     * @param extraDate String {"time":22,"day":"2018-07-14","timeStr":"11:30","duration":"10分钟"} time时间分割索引0-47
     * @return boolean
     */
    public boolean isConsultingTimeValidate(String extraDate, Integer doctorId, Integer userId) {
        JSONObject ed = JSON.parseObject(extraDate);

        // 非法的json字符串数据
        if (ed == null || ed.getString("day") == null || ed.getString("timeStr") == null) {
            messageServices.setError(CoreErrorDict.ERROR_CONSULTING_DATE);
            return false;
        }

        String st = String.format("%s %s", ed.getString("day"), ed.getString("timeStr"));

        // 将字符串转成Date对象
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date consultingDate = sdf.parse(st);
            Date currentDate = new Date();

            // 咨询时间小于当前时间
            if (consultingDate.compareTo(currentDate) < 0) {
                messageServices.setError(CoreErrorDict.ERROR_CONSULTING_DATE);
                return false;
            }

            // 当前合法咨询记录时间是否存在
            List<Byte> invalidStatus = new ArrayList<>();
            // 以下状态可重新预约相同时间的咨询服务 可用状态6,7,9
            invalidStatus.add(BizConstants.Consulting_Status_AutoClose);
            invalidStatus.add(BizConstants.Consulting_Status_Reject);
            invalidStatus.add(BizConstants.Consulting_Status_Refunded);
            invalidStatus.add(BizConstants.Consulting_Status_Paying);

            // 查询当前时间是否有咨询记录
            PlConsultingExample pe = new PlConsultingExample();
            pe.createCriteria().andDoctorIdEqualTo(doctorId)
                    .andUserIdEqualTo(userId)
                    .andStatusNotIn(invalidStatus)
                    .andBeginTimeEqualTo(consultingDate);

            List<PlConsulting> consultingList = consultingMapper.selectByExample(pe);
            if (consultingList.size() > 0) {
                messageServices.setError(CoreErrorDict.ERROR_CONSULTING_DATE_REDUNDANT);
                return false;
            }
        } catch (ParseException e) {
            // 日期解析错误
            messageServices.setError(CoreErrorDict.ERROR_CONSULTING_DATE);
            return false;
        }

        return true;
    }

}
